# Replication Archive for 
# Alexander Coppock, Emily Ekins and David Kirby (2018), 
# "The Long-lasting Effects of Newspaper Op-Eds on Public Opinion",
# Quarterly Journal of Political Science: Vol. 13: No. 1, pp 59-87.

rm(list = ls())

# Uncomment to install
# install.packages(c("tidyverse", "estimatr", "coefplot"))

library(tidyverse)
library(estimatr)
library(coefplot)

# Set working directory to replication archive

load("mturk_opeds_cleaned.rdata")
load("elite_opeds_cleaned.rdata")
load("dv_df.rdata")
source("opeds_source.R")

dvs <-
  c(
    "dv_amtrak_main_w2",
    "dv_climate_main_w2",
    "dv_flat_main_w2",
    "dv_vets_main_w2",
    "dv_wall_main_w2",
    "dv_amtrak_scale_w2",
    "dv_climate_scale_w2",
    "dv_flat_scale_w2",
    "dv_vets_scale_w2",
    "dv_wall_scale_w2"
  )
dv_labels <-
  rep(c("Amtrak", "Climate", "Flat Tax", "Veterans", "Wall Street"), 2)
Zs <-
  rep(c("amtrak", "climate", "paul", "veterans", "wallstreet"), 2)

df <- NULL

for(i in 1:length(dvs)){
    local_formula <- formula(paste0(dvs[i], "~ Z == '", Zs[i], "'"))
    local_formula_int <- formula(paste0(dvs[i], "~ (Z == '", Zs[i], "')*Z_distract"))
    fit_nodistract <- lm_robust(local_formula, data = filter(mturk_opeds, Z_distract == 0))
    fit_distract <- lm_robust(local_formula, data = filter(mturk_opeds, Z_distract == 1))
    fit_both <- lm_robust(local_formula_int, data = mturk_opeds)
    
    summary_df <- 
      bind_rows(
        `No Distraction` = tidy(fit_nodistract)[2, ],
        `Distraction` = tidy(fit_distract)[2, ],
        `Difference` = tidy(fit_both)[4, ],
        .id = "facet"
      ) %>%
      mutate(treatment = Zs[i], 
             sample = "Mechanical Turk")
    
    df <- bind_rows(df, summary_df)
}


# elite sample ------------------------------------------------------------

dvs <- c("dv_amtrak_main_w2", "dv_flat_main_w2", "dv_vets_main_w2", "dv_wall_main_w2",
         "dv_amtrak_scale_w2", "dv_flat_scale_w2", "dv_vets_scale_w2", "dv_wall_scale_w2")
dv_labels <- rep(c("Amtrak", "Flat Tax", "Veterans", "Wall Street"), 2)
Zs <- rep(c("amtrak", "paul", "veterans", "wallstreet"), 2)


for(i in 1:length(dvs)){
  local_formula <- formula(paste0(dvs[i], "~ Z == '", Zs[i], "'"))
  local_formula_int <- formula(paste0(dvs[i], "~ (Z == '", Zs[i], "')*Z_distract"))
  fit_nodistract <- lm_robust(local_formula, data = filter(mturk_opeds, Z_distract == 0))
  fit_distract <- lm_robust(local_formula, data = filter(mturk_opeds, Z_distract == 1))
  fit_both <- lm_robust(local_formula_int, data = mturk_opeds)
  
  summary_df <- 
    bind_rows(
      `No Distraction` = tidy(fit_nodistract)[2, ],
      `Distraction` = tidy(fit_distract)[2, ],
      `Difference` = tidy(fit_both)[4, ],
      .id = "facet"
    ) %>%
    mutate(treatment = Zs[i], 
           sample = "Elites")
  
  df <- bind_rows(df, summary_df)
}


df <-
  df %>%
  mutate(
    dv_type = ifelse(grepl("scale", outcome), "Scale DV", "Main DV"),
    facet = factor(facet, levels = c(
      "No Distraction", "Distraction", "Difference"
    ))
  )



g <-
  ggplot(df,
         aes(
           y = outcome,
           x = estimate,
           group = dv_type,
           color = dv_type,
           shape = dv_type
         )) +
  geom_point(position = position_dodgev(height = .5), size = 2) +
  geom_errorbarh(
    aes(xmin = conf.low, xmax = conf.high),
    position = position_dodgev(height = .5),
    height = 0
  ) +
  theme_bw() +
  geom_vline(xintercept = 0, linetype = "dashed") +
  theme(
    legend.position = "bottom",
    legend.title = element_blank(),
    axis.title = element_blank(),
    legend.key.width = unit(4, "lines"),
    strip.background = element_blank()
  ) +
  ylab("Estimated Treatment Effects") +
  facet_grid(sample ~ facet, scales = "free_y")

g



